﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Itop.KSRY.Model;
using DevExpress.Utils;
using System.Collections;
using System.Threading;

namespace Itop.KSRY {
    public partial class UCksmanKS2 : UserControl {
        public UCksmanKS2() {
            InitializeComponent();
            barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem1_ItemClick);
            barButtonItem2.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem2_ItemClick);
        }
        private string[] djNames = new string[] { "公司机关", "一把手", "书记", "副职", "管理人员", "普通工人" };


        void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //导出
            if(DateTime.Now<new DateTime(2012,5,1))
            export();
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //抽考
            exam();
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            colksbz.Visible = false;
            coljb.Visible = false;
        }

        public void exam() {
            sysset set = DBHelper.SqlMapper.GetOneByKey<sysset>("0");
            if (set == null)
                MessageBox.Show("请先维护抽考参数");
            WaitDialogForm waitdlg =new WaitDialogForm("","开始选取人员,请稍候。。。");
            IList<ksman> list = DBHelper.SqlMapper.GetList<ksman>(null);
            DataTable dt = ConvertHelper.ToDataTable((IList)list, typeof(ksman));
            IList dwlist = DBHelper.SqlMapper.GetList("SelectOneStr", "select distinct dwname from ksman");
            //waitdlg.SetCaption(dwlist.Count + "");
            manlist.Clear();
            int[] djrys = new int[6] ;
            manlist = new List<ksman>();
            string wh1 = "ksbz<>'否' and ";
            if (set.nl > 0)
                wh1 += " nl<" + set.nl + " and ";

            int count=0;
            if (set.dj0 > 0) {
                manlist.AddRange(select(dt.Select(wh1 + "jb=0"), set.dj0));
                djrys[0] = manlist.Count;
                count=manlist.Count;
            }
            if (set.dj1 > 0) {
                manlist.AddRange(select(dt.Select(wh1 + "jb=1"), set.dj1));
                djrys[1] = manlist.Count - count;
                count=manlist.Count;
            }
            if (set.dj2 > 0) {
                //manlist.AddRange(select(dt.Select(wh1 + "jb=2"), set.dj2));
            }
            if (set.dj3 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=3 and dwname='" + dw + "'"), set.dj3));
                djrys[3] = manlist.Count - count;
                count=manlist.Count;

            }
            if (set.dj4 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=4 and dwname='" + dw + "'"), set.dj4));
                djrys[4] = manlist.Count - count;
                count=manlist.Count;
            }
            if (set.dj5 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=5 and dwname='" + dw + "'"), set.dj5));
                djrys[5] = manlist.Count - count;
            }
            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (string str in djNames) {
                if (str!="书记")
                {
                    sb.Append(str + "[" + djrys[i] + "]人,");
                }
                i++;
            }
            barStaticItem1.Caption = sb.ToString();
            List<ksman> list1 = new List<ksman>();
            List<ksman> list2 = new List<ksman>();
            foreach (ksman man in manlist) {
                if (man.jb == 0)
                    list1.Add(man);
                else
                    list2.Add(man);
            }
            i = 1;
            foreach (ksman item in list1) {
                item.orderid = i++;
            }
            i = 1;
            foreach (ksman item in list2) {
                item.orderid = i++;
            }
            ksmanBindingSource.DataSource = list1;
            ksmanBindingSource2.DataSource = list2;
            //Thread.Sleep(1000);
            waitdlg.Close();
        }
        List<ksman> manlist = new List<ksman>();
        private IList<ksman> select(DataRow[] rows, int num) {
            IList<ksman> list = new List<ksman>();
            if (num >= rows.Length) {
                foreach (DataRow row in rows) {
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            } else {
                ArrayList rowlist = new ArrayList(rows);
                int listnum = rowlist.Count;
                Random random=new Random();
                while (num > 0) {
                    DataRow row = rowlist[random.Next(1,listnum--) - 1] as DataRow;
                    rowlist.Remove(row);
                    num--;
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            }
            return list;
        }
        private void export() {
            if (manlist.Count == 0) return;
            WaitDialogForm waitdlg = new WaitDialogForm("", "正在导出数据,请稍候。。。");
            DBHelper.ExportExcel(manlist);
            waitdlg.Close();
        }
    }
}
